var express = require('express');
var router = express.Router();
var DB = require('../config/DB');
/* GET users listing. */
router.post('/', function (req, res, next) {
  // 创建数据库连接
  // DB.connect();
  // 进行用户名的重复校验
  DB.query('select user_name from user where user_name = ?', [req.body.username], function (error, result) {
    if (error) {
      res.send({
        status: 200,
        msg: '[SELECT ERROR] - ' + error.message,
        data: {}
      });
      throw error;
    };
    if (result.length > 0 && result[0].user_name) {
      res.send({
        status: 200,
        msg: result[0].user_name + '已存在，不可以重复的用户名',
        data: {}
      });
      return;
    }
    insertUser();
  });
  //执行数据新增
  function insertUser() {
    var insterSql = 'insert into user(user_name,user_pwd) value (?,?)'
    var addData = [req.body.username, req.body.password];
    DB.query(insterSql, addData, function (error, result) {
      if (error) {
        res.send({
          status: 200,
          msg: '[SELECT ERROR] - ' + error.message,
          data: {}
        });
        throw error;
      };
      res.send({
        status: 200,
        msg: '注册成功',
        data: {}
      });
      return;
    })
  }
});

module.exports = router;